/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var f$ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var removeSelfById=function(id){
    f$(id).parentNode.removeChild(f$(id));
}

function createHidenDiv(id,w,h){
    var p=document.body;
    var _div=document.createElement("div");
    _div.setAttribute("id", id);
    _div.style.position="absolute";
    _div.style.zIndex="1000";
    _div.style.backgroundColor="#ccc";
    _div.style.offsetWidth=w;
    _div.style.offsetHeight=h;
    _div.style.offsetLeft= (p.offsetWidth-_div.style.offsetWidth)/2;
    _div.style.offsetTop= (p.offsetHeight-_div.style.offsetHeight)/2;
    _div.style.display="block";
    return _div;
}

function disposeHidenDiv(id){
 
    f$(id).style.display="none";
    removeSelfById(id);
}

function generateRandom(len,max){
    var arr=[];
    while(arr.length<len){
        var k=Math.round(Math.random()*max);
        if(!isExist(arr,k )){
            arr[arr.length]=k;
        }
    }
    return arr;
}
function isExist(arr,v){
    for(var i=0,len=arr.length;i<len;i++){
        if(arr[i]==v)
            return true;
    }
    return false;
}

function blankRep(times){
    var arr=[];
    for(var i=0;i<times;i++){
        arr.push("&nbsp;");
    }
    return arr.join("");
}

function insertAtCursor(myField, myValue)
{
    //IE support
    if (document.selection)
    {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        sel.select();
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0')
    {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        // save scrollTop before insert
        var restoreTop = myField.scrollTop;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos,myField.value.length);
        if (restoreTop > 0)
        {
            // restore previous scrollTop
            myField.scrollTop = restoreTop;
        }
        myField.focus();
        myField.selectionStart = startPos + myValue.length;
        myField.selectionEnd = startPos + myValue.length;
    } else {
        myField.value += myValue;
        myField.focus();
    }
}
var EventUtil = new Object;
EventUtil.addEventHandler = function (oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
};
        
EventUtil.removeEventHandler = function (oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};

EventUtil.formatEvent = function (oEvent) { 
    if (typeof oEvent.charCode == "undefined") {
        oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
       // oEvent.isChar = (oEvent.charCode > 0);
    }
    if (oEvent.srcElement && !oEvent.target) {
        oEvent.eventPhase = 2;
        oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
        oEvent.pageY = oEvent.clientY + document.body.scrollTop;
        
        if (!oEvent.preventDefault) {
                oEvent.preventDefault = function () {
                    this.returnValue = false;
                };
        }

        if (oEvent.type == "mouseout") {
            oEvent.relatedTarget = oEvent.toElement;
        } else if (oEvent.type == "mouseover") {
            oEvent.relatedTarget = oEvent.fromElement;
        }

        if (!oEvent.stopPropagation) {
                oEvent.stopPropagation = function () {
                    this.cancelBubble = true;
                };
        }
        
        oEvent.target = oEvent.srcElement;
        //oEvent.time = (new Date).getTime();
    }
    return oEvent;
};

EventUtil.getEvent = function(event) {  
  var ev = event?event:window.event;
    if (ev) {
        return this.formatEvent(ev);
    } else {
        return EventUtil.getEvent.caller.arguments[0];
    }
};
